/**
 * 
 */

const WebGLGlobeDataSource = require('./webglglobedatasource');
const InfoCesiumTerrainProvider = require("./InfoCesiumTerrainProvider");
const FreedoTerrainProvider = require("./FreedoTerrainProvider");


/**
 * freedo
 */
function FreeDo() {
    this._viewer = undefined;
    this._performanceDisplay = undefined;
}

/**
 * 初始化场景
 */
FreeDo.prototype.init = function () {

    var viewer = this._viewer = new Cesium.Viewer("cesium", {
        animation: false,
        baseLayerPicker: false,
        fullscreenButton: false,
        geocoder: false,
        homeButton: false,
        infoBox: false,
        sceneModePicker: true,
        selectionIndicator: false,
        timeline: false,
        navigationHelpButton: false,
        navigationInstructionsInitiallyVisible: false,
        imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
            url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
        }),

        terrainProvider: new InfoCesiumTerrainProvider({
            "url": 'http://182.92.7.32:3001/services/wets/wdem_layer/wdem'
        })

        // terrainProvider: new FreedoTerrainProvider({
        //     //"url": 'http://localhost:3001/services/wets/heightmap_layer/world_dem'
        //     //"url": 'https://assets.agi.com/stk-terrain/world'
        //     "url": "http://localhost:9099/",
        //     //requestWaterMask: true
        // })
        // terrainProvider: new Cesium.CesiumTerrainProvider({
        //     //"url": 'https://assets.agi.com/stk-terrain/world'
        //     "url": "http://localhost:9099/"
        // })
    });

};


FreeDo.prototype.addTiles = function () {

    var tileset = this._viewer.scene.primitives.add(new Cesium.Cesium3DTileset({
        //url: 'http://localhost:8001/tilesets/TilesetWithTreeBillboards/'
        url: 'https://cesiumjs.org/NewYork/3DTilesGml'
    }));

    tileset.readyPromise.then(function (tileset) {
        this._viewer.viewer.camera.viewBoundingSphere(tileset.boundingSphere, new Cesium.HeadingPitchRange(0, -0.5, 0));
        this._viewer.viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
    });
}

/**
 * webglglobe数据源
 */
FreeDo.prototype.addDataSource = function () {
    var _this = this;
    var dataSource = new WebGLGlobeDataSource();
    dataSource.loadUrl('/data/population909500.json').then(function () {
        _this._viewer && _this._viewer.dataSources.add(dataSource);
    });
};


/**
 * 跟踪显示目标点经纬高
 */
FreeDo.prototype.setPick = function () {
    var viewer = this._viewer;
    var scene = viewer.scene;
    var entity = viewer.entities.add({
        label: {
            show: false,
            showBackground: true,
            font: '14px monospace',
            horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
            verticalOrigin: Cesium.VerticalOrigin.TOP,
            pixelOffset: new Cesium.Cartesian2(15, 0)
        }
    });

    handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
    handler.setInputAction(function (movement) {
        //var cartesian = viewer.camera.pickEllipsoid(movement.endPosition, scene.globe.ellipsoid);
        var ray = viewer.camera.getPickRay(movement.endPosition);
        var position = viewer.scene.globe.pick(ray, viewer.scene);
        if (Cesium.defined(position)) {
            var cartographic = Cesium.Ellipsoid.WGS84.cartesianToCartographic(position);
            var altitudeString = cartographic.height.toFixed(2);
            //var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
            var longitudeString = Cesium.Math.toDegrees(cartographic.longitude).toFixed(2);
            var latitudeString = Cesium.Math.toDegrees(cartographic.latitude).toFixed(2);

            entity.position = position;
            entity.label.show = true;
            entity.label.text =
                'Lon: ' + ('  ' + longitudeString).slice(-7) +
                '\nLat: ' + ('  ' + latitudeString).slice(-7) +
                '\nAlt: ' + '  ' + altitudeString;
        } else {
            entity.label.show = false;
        }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
}

/**
 * 显示帧率
 */
FreeDo.prototype.showPerformanceDisplay = function () {

    var perforanceDisplay = this._performanceDisplay = new Cesium.PerformanceDisplay({
        container: document.getElementById('perferance')
    })

    this._viewer.scene.postRender.addEventListener(function () {
        perforanceDisplay.update();
    });

};


FreeDo.prototype.showWireFrame = function () {
    this._viewer.scene.globe._surface.tileProvider._debug.wireframe = true;
}


FreeDo.prototype.addCesiumInspector = function () {

    this._viewer.scene.globe.depthTestAgainstTerrain = true;
    this._viewer.extend(Cesium.viewerCesiumInspectorMixin);
}

module.exports = FreeDo;
